package com.smarth.zhizulian.utils;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by 18865 on 2018/1/18.
 */

public class AESUtils {
    public final static String APPKEY = "0492";
    public static final String VIPARA = "0102030405060708";
    public static final String bm = "utf-8";

    public static String aes(String dataPassword, String cleartext)
            throws Exception {
        IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes());
        SecretKeySpec key = new SecretKeySpec(dataPassword.getBytes(), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv);
        byte[] encryptedData = cipher.doFinal(cleartext.getBytes(bm));
        return Base64Utils.encode(encryptedData);
    }

    public static String encrypt(String str, String myKey) {
        try {
            return aes(APPKEY + myKey, str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "encryptError";
    }

}
